<?php

class m110903_153751_init_user extends CDbMigration {

	public function safeUp() {
		$this
			->execute(
				'
create table `erp_auth_item`
(
   `name`                 varchar(64) not null,
   `type`                 integer not null,
   `description`          text,
   `bizrule`              text,
   `data`                 text,
   primary key (`name`)
) engine InnoDB;');
		$this
			->execute(
				'
create table `erp_auth_item_child`
(
   `parent`               varchar(64) not null,
   `child`                varchar(64) not null,
   primary key (`parent`,`child`),
   foreign key (`parent`) references `erp_auth_item` (`name`) on delete cascade on update cascade,
   foreign key (`child`) references `erp_auth_item` (`name`) on delete cascade on update cascade
) engine InnoDB;
');
		$this
			->execute(
				'
create table `erp_auth_assignment`
(
   `itemname`             varchar(64) not null,
   `userid`               varchar(64) not null,
   `bizrule`              text,
   `data`                 text,
   primary key (`itemname`,`userid`),
   foreign key (`itemname`) references `erp_auth_item` (`name`) on delete cascade on update cascade
) engine InnoDB;');
		$this
			->createTable('erp_user',
				array(
					'id' => 'pk',
					'username' => 'string',
					'password' => 'string',
					'active' => 'boolean',
				));
		$this
			->insert('erp_user',
				array(
					'username' => 'admin',
					'password' => '09d2e674bd90235ae2f5652799e4e0cad2ea29f08ba16c5f45fd73173fc696eef3929a6b4e9e0143b398d80f73af38f3a469ebf8543a1e1739a2a1759e84046b',
					'active' => 1,
				));
		$this->createIndex("user_username", 'erp_user', 'username');
		$this
			->createTable('erp_employee',
				array(
					'id' => 'pk',
					'userId' => 'int',
					'familyName' => 'string',
					'givenName' => 'string',
					'employed' => 'boolean',
				));
		$this->createIndex("employee_user_id", 'erp_employee', 'userId');
		$this
			->addForeignKey('fk_employee_user', 'erp_employee', 'userId', 'erp_user', 'id',
				'SET NULL', 'CASCADE');

		$this
			->createTable('erp_user_access',
				array(
					'id' => 'pk',
					'userId' => 'int',
					'employeeAdmin' => 'boolean',
				));
		$this->createIndex("user_access_user", 'erp_user_access', 'userId');
		$this
			->addForeignKey('fk_user_access_user', 'erp_user_access', 'userId', 'erp_user', 'id',
				'CASCADE', 'CASCADE');

		$this
			->insert('erp_auth_item',
				array(
					'name' => 'admin',
					'type' => CAuthItem::TYPE_ROLE
				));
		$this->insert('erp_user_access', array(
				'userId' => 1,
			));
		$this
			->insert('erp_auth_item',
				array(
					'name' => 'employee',
					'type' => CAuthItem::TYPE_ROLE,
					'bizrule' => 'return AccessControl::checkEmployee();',
				));
		$this
			->insert('erp_auth_item',
				array(
					'name' => 'employeeAdmin',
					'type' => CAuthItem::TYPE_ROLE,
					'bizrule' => 'return AccessControl::check("employeeAdmin");',
				));
		$this
			->insert('erp_auth_item_child',
				array(
					'parent' => 'employee',
					'child' => 'employeeAdmin',
				));

		$this
			->insert('erp_auth_item_child',
				array(
					'parent' => 'admin',
					'child' => 'employeeAdmin',
				));
		$this
			->insert('erp_auth_assignment',
				array(
					'itemname' => 'admin',
					'userid' => 1,
				));
	}

	public function safeDown() {
		$this->dropTable('erp_user_access');
		$this->dropTable('erp_employee');
		$this->dropTable('erp_user');
		$this->execute('drop table if exists `erp_auth_assignment`;');
		$this->execute('drop table if exists `erp_auth_item_child`;');
		$this->execute('drop table if exists `erp_auth_item`;');
	}
}
